package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.CurePigDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskCourseDAO;
import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.PigDAO;

import cn.edu.scau.cmi.hombio.domain.CurePig;
import cn.edu.scau.cmi.hombio.domain.CureTaskCourse;
import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Pig;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for CurePig entities
 * 
 */

@Service("CurePigService")

@Transactional
public class CurePigServiceImpl implements CurePigService {

	/**
	 * DAO injected by Spring that manages CurePig entities
	 * 
	 */
	@Autowired
	private CurePigDAO curePigDAO;

	/**
	 * DAO injected by Spring that manages CureTaskCourse entities
	 * 
	 */
	@Autowired
	private CureTaskCourseDAO cureTaskCourseDAO;

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * Instantiates a new CurePigServiceImpl.
	 *
	 */
	public CurePigServiceImpl() {
	}

	/**
	 */
	@Transactional
	public CurePig findCurePigByPrimaryKey(Integer id) {
		return curePigDAO.findCurePigByPrimaryKey(id);
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public CurePig deleteCurePigPig(Integer curepig_id, Integer related_pig_id) {
		CurePig curepig = curePigDAO.findCurePigByPrimaryKey(curepig_id, -1, -1);
		Pig related_pig = pigDAO.findPigByPrimaryKey(related_pig_id, -1, -1);

		curepig.setPig(null);
		related_pig.getCurePigs().remove(curepig);
		curepig = curePigDAO.store(curepig);
		curePigDAO.flush();

		related_pig = pigDAO.store(related_pig);
		pigDAO.flush();

		pigDAO.remove(related_pig);
		pigDAO.flush();

		return curepig;
	}

	/**
	 * Delete an existing CurePig entity
	 * 
	 */
	@Transactional
	public void deleteCurePig(CurePig curepig) {
		curePigDAO.remove(curepig);
		curePigDAO.flush();
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public CurePig saveCurePigPig(Integer id, Pig related_pig) {
		CurePig curepig = curePigDAO.findCurePigByPrimaryKey(id, -1, -1);
		Pig existingpig = pigDAO.findPigByPrimaryKey(related_pig.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpig != null) {
			existingpig.setId(related_pig.getId());
			existingpig.setNumber(related_pig.getNumber());
			existingpig.setEarNumber(related_pig.getEarNumber());
			existingpig.setRfid(related_pig.getRfid());
			existingpig.setBirthday(related_pig.getBirthday());
			existingpig.setState(related_pig.getState());
			existingpig.setMemo(related_pig.getMemo());
			related_pig = existingpig;
		}

		curepig.setPig(related_pig);
		related_pig.getCurePigs().add(curepig);
		curepig = curePigDAO.store(curepig);
		curePigDAO.flush();

		related_pig = pigDAO.store(related_pig);
		pigDAO.flush();

		return curepig;
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public CurePig deleteCurePigEmployee(Integer curepig_id, Integer related_employee_id) {
		CurePig curepig = curePigDAO.findCurePigByPrimaryKey(curepig_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		curepig.setEmployee(null);
		related_employee.getCurePigs().remove(curepig);
		curepig = curePigDAO.store(curepig);
		curePigDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return curepig;
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public CurePig saveCurePigEmployee(Integer id, Employee related_employee) {
		CurePig curepig = curePigDAO.findCurePigByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		curepig.setEmployee(related_employee);
		related_employee.getCurePigs().add(curepig);
		curepig = curePigDAO.store(curepig);
		curePigDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return curepig;
	}

	/**
	 * Save an existing CurePig entity
	 * 
	 */
	@Transactional
	public void saveCurePig(CurePig curepig) {
		CurePig existingCurePig = curePigDAO.findCurePigByPrimaryKey(curepig.getId());

		if (existingCurePig != null) {
			if (existingCurePig != curepig) {
				existingCurePig.setId(curepig.getId());
				existingCurePig.setCureTime(curepig.getCureTime());
				existingCurePig.setMemo(curepig.getMemo());
			}
			curepig = curePigDAO.store(existingCurePig);
		} else {
			curepig = curePigDAO.store(curepig);
		}
		curePigDAO.flush();
	}

	/**
	 * Save an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public CurePig saveCurePigCureTaskCourse(Integer id, CureTaskCourse related_curetaskcourse) {
		CurePig curepig = curePigDAO.findCurePigByPrimaryKey(id, -1, -1);
		CureTaskCourse existingcureTaskCourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(related_curetaskcourse.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureTaskCourse != null) {
			existingcureTaskCourse.setId(related_curetaskcourse.getId());
			existingcureTaskCourse.setStep(related_curetaskcourse.getStep());
			existingcureTaskCourse.setTime(related_curetaskcourse.getTime());
			existingcureTaskCourse.setMemo(related_curetaskcourse.getMemo());
			related_curetaskcourse = existingcureTaskCourse;
		}

		curepig.setCureTaskCourse(related_curetaskcourse);
		related_curetaskcourse.getCurePigs().add(curepig);
		curepig = curePigDAO.store(curepig);
		curePigDAO.flush();

		related_curetaskcourse = cureTaskCourseDAO.store(related_curetaskcourse);
		cureTaskCourseDAO.flush();

		return curepig;
	}

	/**
	 * Return all CurePig entity
	 * 
	 */
	@Transactional
	public List<CurePig> findAllCurePigs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<CurePig>(curePigDAO.findAllCurePigs(startResult, maxRows));
	}

	/**
	 * Return a count of all CurePig entity
	 * 
	 */
	@Transactional
	public Integer countCurePigs() {
		return ((Long) curePigDAO.createQuerySingleResult("select count(o) from CurePig o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public CurePig deleteCurePigCureTaskCourse(Integer curepig_id, Integer related_curetaskcourse_id) {
		CurePig curepig = curePigDAO.findCurePigByPrimaryKey(curepig_id, -1, -1);
		CureTaskCourse related_curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(related_curetaskcourse_id, -1, -1);

		curepig.setCureTaskCourse(null);
		related_curetaskcourse.getCurePigs().remove(curepig);
		curepig = curePigDAO.store(curepig);
		curePigDAO.flush();

		related_curetaskcourse = cureTaskCourseDAO.store(related_curetaskcourse);
		cureTaskCourseDAO.flush();

		cureTaskCourseDAO.remove(related_curetaskcourse);
		cureTaskCourseDAO.flush();

		return curepig;
	}

	/**
	 * Load an existing CurePig entity
	 * 
	 */
	@Transactional
	public Set<CurePig> loadCurePigs() {
		return curePigDAO.findAllCurePigs();
	}
}
